package com.erp.order.supply.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.hr.base.util.CommonUtils;
import com.erp.order.purchasing.model.InquiryList;
import com.erp.order.supply.dao.SupplyMaterialRDao;
import com.erp.order.supply.model.SupplyMaterialR;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SupplyMaterialRDaoImpl extends BaseDaoImpl<SupplyMaterialR> implements SupplyMaterialRDao {
    /**
     * dao支持
     */
    @Autowired
    private DaoSupport daoSupport;

    @Override
    public SupplyMaterialR getDataObject(String s) {
        return null;
    }

    @Override
    public List<SupplyMaterialR> getDataObjects(SupplyMaterialR supplyMaterialR) {
        return null;
    }

    @Override
    public List<SupplyMaterialR> getDataObjectsForDataAuth(String s, Pages pages, SupplyMaterialR supplyMaterialR) {
        return null;
    }

    @Override
    public SupplyMaterialR addCompleteData(SupplyMaterialR supplyMaterialR) {
        this.daoSupport.insertDataTransaction(supplyMaterialR);
        return supplyMaterialR;
    }

    @Override
    public List<SupplyMaterialR> getDatasByOrderId(Long id) {
        List<SupplyMaterialR> result = daoSupport.selectDataSql(
                "SELECT " +
                        "il.* " +
                        "FROM " +
                        "supply_material_r il " +
                        "WHERE " +
                        "il.purchase_order_id = :id",
                new HashMap<String, Class<?>>(){{
                    put("il", SupplyMaterialR.class);
                }},
                new HashMap<String, Object>(){{
                    put("id", id);
                }});
        return result;
    }

    @Override
    public Integer getCreateCount() {
        String sql =
                "SELECT " +
                        "il.* " +
                        "FROM " +
                        "supply_material_r il " +
                        "WHERE " +
                        "il.is_del = :isDel " +
                        "AND " +
                        "DATE_FORMAT(il.create_time, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')";
        Map<String, Object> args = new HashMap<>();
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("il", SupplyMaterialR.class);
        List<InquiryList> result = daoSupport.selectDataSql(sql, entity, args);
        return result == null ? 0 : result.size();
    }

    @Override
    public SupplyMaterialR getDataObjectById(String valueOf) {
        String sql =
                "SELECT " +
                        "smr.* " +
                        "FROM " +
                        "supply_material_r smr " +
                        "WHERE " +
                        "smr.id = :id ";
        Map<String, Object> args = new HashMap<>();
        args.put("id",valueOf );
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("smr", SupplyMaterialR.class);
        List<SupplyMaterialR> result = daoSupport.selectDataSql(sql, entity, args);
        return result == null ? null : result.get(0);
    }
}
